<%=packageName ? "package ${packageName}\n\n" : ''%>

import grails.converters.XML
import grails.converters.JSON

class ${className}Controller {

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    //static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        def model = [ ${propertyName}List: ${className}.list( params ), ${propertyName}Total: ${className}.count() ]
        withFormat{
            html{
                return model
            }
            xml{
                render model as XML
            }
            json{
                render model as JSON
            }
        }


    }



    def edit = {
        def ${propertyName} = ${className}.get( params.id )

        if(!${propertyName}) {
            flash.message = "${className} not found with id \${params.id}"
            redirect(action:list)
        }
        else {
            return [ ${propertyName} : ${propertyName} ]
        }
    }



    def create = {
        def ${propertyName} = new ${className}()
        ${propertyName}.properties = params
        return ['${propertyName}':${propertyName}]
    }


 /**
    * rest actions
  */

    def show = {
        def ${propertyName} = ${className}.get( params.id )

        if(!${propertyName}) {
            final  notFoundMessage = "${className} not found with id \${params.id}"
            withFormat{
                html{

                    flash.message = notFoundMessage
                    redirect(action:list)
                }
                xml{
                    response.status = 404
                    render(view:"/errorxml",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(notFoundMessage)])
                }
                json{
                    response.status = 404
                    render(view:"/errorjson",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(notFoundMessage)])
                }
            }

        }
        else {
            withFormat{
                html{
                    return [ ${propertyName} : ${propertyName} ]
                }
                xml{
                    render ${propertyName} as XML
                }
                json{
                    render ${propertyName} as JSON
                }
            }

        }
    }

    def save = {
        def bindData = params.${className.toLowerCase()}?params.${className.toLowerCase()}:params
        def ${propertyName} = new ${className}(bindData)
        if(!${propertyName}.hasErrors() && ${propertyName}.save()) {
            final String createSucessMsg = "${className} \${${propertyName}.id} created"
            withFormat{
                form{
                    flash.message = createSucessMsg
                    redirect(action:show,id:${propertyName}.id)
                }
                xml{
                    response.status = 201 //created
                    render ${propertyName} as XML
                }
                json{
                    response.status = 201 //created
                    render ${propertyName} as JSON
                }
            }


        }
        else {
            withFormat{
                form{
                    render(view:'create',model:[${propertyName}:${propertyName}])
                }
                xml{
                    response.status = 500
                    render ${propertyName}.errors as XML
                }
                json{
                    response.status = 500
                    render ${propertyName}.errors as JSON
                }
            }

        }
    }

    def update = {
        def ${propertyName} = ${className}.get( params.id )
        if(${propertyName}) {
            if(params.version) {
                def version = params.version.toLong()
                if(${propertyName}.version > version) {
                    withFormat{
                        form{
                            <%def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className)%>
                            ${propertyName}.errors.rejectValue("version", "${lowerCaseName}.optimistic.locking.failure", "Another user has updated this ${className} while you were editing.")
                            render(view:'edit',model:[${propertyName}:${propertyName}])
                        }

                        xml{
                            response.status = 500
                            render ${propertyName}.errors as XML
                        }
                        json{
                            response.status = 500
                            render ${propertyName}.errors as JSON
                        }

                    }

                }
            }
            ${propertyName}.properties = params
            if(!${propertyName}.hasErrors() && ${propertyName}.save()) {
                final String updateSuccessMsg = "${className} \${params.id} updated"
                withFormat{
                    form{
                        flash.message = updateSuccessMsg
                        redirect(action:show,id:${propertyName}.id)
                    }
                    xml{
                        render ${propertyName} as XML
                    }
                    json{
                        render ${propertyName} as JSON
                    }
                }


            }
            else {
                withFormat{
                    form{
                        render(view:'edit',model:[${propertyName}:${propertyName}])
                    }
                    xml{
                        response.status = 500
                        render ${propertyName}.errors as XML
                    }
                    json{
                        response.status = 500
                        render ${propertyName}.errors as JSON
                    }
                }

            }
        }
        else {
            final String updateNotFoundMsg = "${className} not found with id \${params.id}"
            withFormat{
                form{
                    flash.message = updateNotFoundMsg
                    redirect(action:edit,id:params.id)
                }
                xml{
                    response.status = 404
                    render(view:"/errorxml",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(updateNotFoundMsg)])
                }
                json{
                    response.status = 404
                    render(view:"/errorjson",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(updateNotFoundMsg)])
                }
            }

        }
    }

    def delete = {
        def ${propertyName} = ${className}.get( params.id )

        if(${propertyName}) {
            try {
                final String delSuccessMsg = "${className} \${params.id} deleted"
                ${propertyName}.delete()
                withFormat{
                    form{
                        flash.message = delSuccessMsg
                        redirect(action:list)
                    }
                    xml{
                        render delSuccessMsg
                    }
                    json{
                        render delSuccessMsg
                    }
                }

            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                final String delFailMsg = "${className} \${params.id} could not be deleted"
                withFormat{
                    form{
                        flash.message = delFailMsg
                        redirect(action:show,id:params.id)
                    }
                    xml{
                        response.status = 409
                        render(view:"/errorxml",
                                model:[errorCode:"409",
                                        exception:new ResourceNotFoundException(delFailMsg)])
                    }
                    json{
                        response.status = 409
                        render(view:"/errorjson",
                                model:[errorCode:"409",
                                        exception:new ResourceNotFoundException(delFailMsg)])
                    }
                }
            }
        }
        else {
            final String delNotFoundMsg = "${className} not found with id \${params.id}"
            withFormat{
                form{
                    flash.message = delNotFoundMsg
                    redirect(action:list)
                }
                xml{
                    response.status = 404
                    render(view:"/errorxml",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(delNotFoundMsg)])
                }
                json{
                    response.status = 404
                    render(view:"/errorjson",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(delNotFoundMsg)])
                }
            }


        }
    }
}
